using System;
using System.Globalization;
using System.Collections;
using UnityEngine;
using UnityEditor.Animations;
using System.IO.Ports;
// 角色基类，包含角色的属性和基本功能
public class CharacterBase<T> : CharacterBase where T : CharacterBase<T>
{
    public Vector3 _moveForward;
    public float rotateLerpSpeed = 5f;
    public CharacterAnimation CharacterAnimation;
    public float moveSpeed;
    public Rigidbody2D rb { get; private set; }
    public CharacterStateMachine<T> characterStateMachine;
    public IInputSource InputSource;
    private DataController dataController;
    private Quaternion rotation;
    public new bool die { get; private set; }
    public GameObject swordObject;
    public int hitForce;

    void Awake()
    {
        //实例化
        Init();
        CharacterAnimation = GetComponent<CharacterAnimation>();
        rb = GetComponent<Rigidbody2D>();
        characterStateMachine = new CharacterStateMachine<T>();
        dataController = GetComponent<DataController>();
    }

    private void Start()
    {
        closeDamage();
        onStart();
    }

    protected virtual void onStart() { }

    void OnEnable()
    {
        Enable();
    }
    protected virtual void Enable() { }
    void OnDisable()
    {
        Disable();
    }
    protected virtual void Disable() { }
    public virtual void MoveCharacter() { }

    void Update()
    {
        onUpdate();
        // mainCamera.transform.position = new Vector3(0, 0, -2);
        characterStateMachine.Update();

        // if (!(characterStateMachine.CurrentState is CharacterJumpState<T>) || !(characterStateMachine.CurrentState is CharacterAttackState<T>))
        // rb.angularVelocity = Vector3.zero;

        // ExitStop();
        var state = GetCurrentState() as CharacterState<PlayerController>;
        if (state != null)
        {
            CharacterAnimation.currentState = state;
        }
        InputSource.onUpdate();
    }
    protected virtual void Init() { }
    protected virtual void onUpdate() { }
    void FixedUpdate()
    {
        onFixedUpdate();
        characterStateMachine.FixedUpdate();
    }

    protected virtual void onFixedUpdate() { }

    //角色停止所有动作
    public void StopAllActions()
    {
        if (rb == null) return;
        rb.velocity = Vector3.zero;
        rb.drag = 0;
        // 只清水平速度，保留必要的下落
        Vector3 v = rb.velocity;
        rb.velocity = new Vector3(0f, Mathf.Min(v.y, 0f), 0f);
        StartCoroutine(SlerpTo(rotation, 0.15f));
    }


    private IEnumerator SlerpTo(Quaternion target, float dur) // 平滑协程
    {
        Quaternion from = transform.rotation;         // 起始旋转
        float t = 0f;                                  // 归一化时间
        while (t < 1f)
        {                              // 持续到达1
            t += Time.deltaTime / dur;                // 线性推进
            transform.rotation = Quaternion.Slerp(from, target, t); // 四元数插值
            yield return null;                        // 等一帧
        }
        transform.rotation = target;                  // 末帧对齐
    }


    void OnDrawGizmos()
    {
        Vector2 start = transform.position + Vector3.up * 0.1f;
        Vector2 end = start + Vector2.down * 0.01f;
        Gizmos.color = Color.red;
        Gizmos.DrawRay(start, Vector2.down * 0.01f);
    }
    public CharacterState<T> GetCurrentState()
    {
        return characterStateMachine.CurrentState;
    }


    public void setRotation()
    {
        rotation = transform.rotation;
    }

    public void setRotationToThis()
    {
        Action sayHi = delegate
        {
            transform.rotation = rotation;
        };
        if (transform.rotation != rotation)
        {
            sayHi();
        }
    }
    public override void setDieState()
    {
        die = true;
    }
    public void CharacterDie()
    {
        // 设置 Tag 为 Untagged
        gameObject.tag = "Untagged";
        // 设置 Layer 为 Default (0)
        gameObject.layer = 0;
        if (rb != null) rb.isKinematic = true;
        foreach (Collider2D col in GetComponentsInChildren<Collider2D>())
        {
            col.enabled = false;
        }
        Die();
    }
    public virtual void Die() { }

    public override void Hit()
    {
        characterStateMachine.ChangeState(new CharacterHitState<T>((T)this, characterStateMachine)); // 播完回Idle
    }

    public virtual void AttackRepel()
    {

    }
    //从外部关闭 攻击的无敌帧
    public void closeDamage()
    {
        isDamage = false;
    }
}
